const app = getApp()
var common = require("../../common.js")
Page({
  onShow: function () {

  },

  /**
   * 页面的初始数据
   */
  data: {    
    hasPrizeWithoutBet:false,
    hasPrizeWithBet:false,
    gid:0,//游戏
  },
  createNew:function(){
    wx.navigateTo({      
      url: '../../pages/index/index',
    })
  },
  homeTap:function(){
    wx.navigateTo({
      url: '../../pages/suren/home',
    })
  },
  beginGame:function(){    
    if(this.data.gtype=='single'){
      wx.navigateTo({
        url: '../../pages/suren/play?id='+this.data.gid,
      })
    }else{
      wx.navigateTo({
        url: '../../pages/suren/playGroup?id=' + this.data.gid,
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that=this;
    if(options.id){
      this.data.gid = options.id;
    }else{
      this.data.gid = decodeURIComponent(options.scene);
    }
    console.log(options);
    console.log(options.scene);
    console.log("----------=============--------");
    console.log(this.data.gid);

    wx.showLoading({
      title: '正在获取游戏..',
    })
    common.request({ 
      url: 'c=Game&a=getOtherGameDetail', 
      data: { gid: this.data.gid }, 
      success: function (e) { 
        var data=e;
        if (data.status != 0 || e.detail.audit != 1 || e.detail.hasPay ==0){
          wx.showModal({
            content: "游戏未创建或未能通过审核",
            showCancel: false,
            complete:function(){
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          })
          return false;
        } else if (e.detail.hasPay == 2){
          e.detail.isBet=0;
          wx.showModal({
            //title: '提示',
            content: "游戏失效。\r\n游戏创建者已经撤回了红包。",
            showCancel:false,
            success:function(res){
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          })
        }

        
        var relay=true
        if (e.detail.relay != "1") {//禁止转发
          wx.hideShareMenu({});
          relay=false
        }        
        
        var hasPrizeWithoutBet=true;
        var hasPrizeWithBet=false;
          if (e.detail.isBet==1){
            hasPrizeWithBet = true;
            hasPrizeWithoutBet = false;
          }
        that.setData({
          gtype:e.detail.type,
          nickname: e.detail.nickname,
          avatarUrl: e.detail.headPic,
          target: e.detail.target,
          hasPrizeWithoutBet: hasPrizeWithoutBet,
          hasPrizeWithBet: hasPrizeWithBet,
          relay: relay,
          hasJoin:e.detail.hasJoin
        });

      }, 
      complete:function(){
        wx.hideLoading();
      },      
      method: "POST" });
  },



//支付押金
  submitForm: function (e) {
    var that = this;
    var data = {
      gid: that.data.gid
    }
    wx.showLoading({
      title: '正在提交...',
    })
    common.request(
      {
        url: 'c=Game&a=wagerOrder',
        data: data,
        success: function (re) {
          console.log(re);
          if (re.status == 0) {
            that.pay(re.fee, re.wid.toString());
          } else {
            wx.showModal({
              title: '出现错误',
              content: re.msg,
            })
          }
        },
        fail: function (re) { },
        complete: function (re) { }
      }
    );
  },
  pay: function (fee, wid) {//fee单位：元
    var that=this;
    var payInfo = {
      body: '找我唷游戏保证金',
      total_fee: fee,
      order_sn: 'w_'+wid
    }
    this.basePay(payInfo, function (e) { 
      wx.showLoading({
        title: '正在确认支付情况',
      })
      common.request({
        url: 'c=Index&a=checkPayStatus',
        data: { 'id': wid, 'type': 'w' },
        success: function (e) {
          if(e.status==0 && e.payStatus==1){
            wx.navigateTo({
              url: '../../pages/suren/play?id=' + that.data.gid
            })
          }
        },
        complete:function(){
          wx.hideLoading();
        }
      })

     }, function (e) { console.log(e) });
  },

  /** 
 * 支付函数 
 * @param  {[type]} _payInfo [description] 
 * @return {[type]}          [description] 
 */
  basePay: function (_payInfo, success, fail) {
    var payInfo = {
      body: '',
      total_fee: 0,
      order_sn: ''
    }
    Object.assign(payInfo, _payInfo);
    if (payInfo.body.length == 0) {
      wx.showToast({
        title: '支付信息描述错误'
      })
      return false;
    }
    if (payInfo.total_fee == 0) {
      wx.showToast({
        title: '支付金额不能0'
      })
      return false;
    }
    if (payInfo.order_sn.length == 0) {
      wx.showToast({
        title: '订单号不能为空'
      })
      return false;
    }
    var This = this;

    payInfo.openid = app.globalData.openId;
    common.request({
      url: 'c=Pay&a=prepay',
      data: payInfo,
      success: function (res) {
        if (!res.status) {
          wx.showToast({
            title: res['errmsg']
          })
          return false;
        }
        var data = res.data;
        console.clear()
        console.log(data);

        common.request({
          url: 'c=Pay&a=pay',
          data: { prepay_id: data.data.prepay_id },
          success: function (_payResult) {
            wx.hideLoading();
            var payResult = _payResult;
            wx.requestPayment({
              'timeStamp': payResult.timeStamp.toString(),
              'nonceStr': payResult.nonceStr,
              'package': payResult.package,
              'signType': payResult.signType,
              'paySign': payResult.paySign,
              'success': function (succ) {
                success && success(succ);
              },
              'fail': function (err) {
                fail && fail(err);
              },
              'complete': function (comp) {

              }
            })
          }
        })
      }
    })

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '厉害了，找到"' + this.data.target+'",还有机会赢取红包奖励',
      imageUrl:'../../image/f.jpg',
      path: 'pages/suren/begin?id='+this.data.gid,
      success:function(res){
        console.log('shareReturn',res);
      },
      fail:function(ret){
        console.log('cancelShare',ret);
      }
    }
  },
})